package com.zcn.io.web.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcn.io.common.exception.ServiceException;
import com.zcn.io.common.util.DateUtil;
import com.zcn.io.web.api.entity.AnswerRecordTask;
import com.zcn.io.web.api.entity.AnswerRecordTaskDtl;
import com.zcn.io.web.api.query.AnswerRecordTaskQuery;
import com.zcn.io.web.api.vo.AnswerRecordTaskDtlOrderVo;
import com.zcn.io.web.mapper.AnswerRecordTaskMapper;
import com.zcn.io.web.utils.SingleThreadExecutor;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @desc:
 * @author: 202438 邱建伟
 * @date: 2024/11/3 17:51
 * @Copyright (c) 2024, 江苏金恒信息科技股份有限公司
 */
@Service
@AllArgsConstructor
public class AnswerRecordTaskService {

    private final AnswerRecordTaskMapper answerRecordTaskMapper;

    private final AnswerRecordTaskDtlService answerRecordTaskDtlService;
    private final SingleThreadExecutor singleThreadExecutor = new SingleThreadExecutor();

    public void list(Page<AnswerRecordTask> page, AnswerRecordTask orderMain) {
        LambdaQueryWrapper<AnswerRecordTask> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(AnswerRecordTask::getExecuteTime);
        answerRecordTaskMapper.selectPage(page, wrapper);
    }

    public void save(AnswerRecordTask orderMain) {
        orderMain.setTaskStatus("新建");
        orderMain.setExecuteTime(DateUtil.getDefaultDate());
        answerRecordTaskMapper.insert(orderMain);
    }

    public void updateById(AnswerRecordTask orderMain) {
        answerRecordTaskMapper.updateById(orderMain);
    }

    public void updateStatusById(String id, String status) {
        this.answerRecordTaskMapper.updateStatus(id, status);
    }

    /**
     * @desc: 开启一个线程池
     * @author 202438 邱建伟
     * @date 2024/11/3 16:02
     */
    @Transactional
    public String startAnswerRecord(AnswerRecordTaskQuery query) {
        if (singleThreadExecutor.isRunning()) {
            throw new ServiceException("当前有其他任务正在进行中，请稍后再试...");
        }
        //更新任务状态为进行中
        this.updateStatusById(query.getId(), "进行中");
        singleThreadExecutor.execute(() -> {
            processInner(query);
        });
        return "电话接听记录任务接收成功";
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void processInner(AnswerRecordTaskQuery query) {
        try {
            //1.获取当前时间段所有的信息
            List<AnswerRecordTaskDtl> saveList = answerRecordTaskMapper.selectAnswerRecordCount(query);
            //2.处理主档id
            DefaultIdentifierGenerator defaultIdentifierGenerator = new DefaultIdentifierGenerator();
            saveList.forEach(answerRecordTaskDtl -> {
                answerRecordTaskDtl.setMainId(query.getId());
                answerRecordTaskDtl.setId(defaultIdentifierGenerator.nextId(answerRecordTaskDtl).toString());
            });
            //3.删除所有
            answerRecordTaskDtlService.deleteAllByMainId(query.getId());
            //4.保存
            answerRecordTaskDtlService.saveAll(saveList);
            this.updateStatusById(query.getId(), "成功");
        } catch (Exception e) {
            this.updateStatusById(query.getId(), "失败");
            e.printStackTrace();
        }
    }

    public void listByTaskId(Page<AnswerRecordTaskDtlOrderVo> page, AnswerRecordTaskQuery query) {
        answerRecordTaskMapper.listByTaskId(page, query);
    }

    public void deleteById(String id) {
        answerRecordTaskMapper.deleteById(id);
        //级联删除明细信息
        answerRecordTaskDtlService.deleteAllByMainId(id);
    }
}
